/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.os.rh.telas;

import br.os.rh.funcionario.Funcionario;
import br.os.rh.funcionario.FuncionarioDAO;
import br.os.rh.pontoprofessores.PontoProfessores;
import br.os.rh.pontoprofessores.PontoProfessoresDAO;
import br.os.rh.util.Ativo;
import br.os.rh.util.Util;
import groovy.lang.Closure;
import groovy.swing.SwingBuilder;
import java.awt.Image;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author 'Pedro
 */
public class TelaMarcarPonto extends javax.swing.JDialog {

    private Funcionario f;
    private String justificativa = "";

    /**
     * Creates new form TelaMarcarPonto
     */
    public TelaMarcarPonto() {
        initComponents();
        setModal(true);
        setLocationRelativeTo(null);

        lblNome.setText("");
        lblCidade.setText("");
        lblTitulacao.setText("");

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    String fHora = "HH:mm:ss";
                    SimpleDateFormat sdf = new SimpleDateFormat(fHora);
                    Date hour = new Date();

                    String fData = "dd/MM/yy";
                    SimpleDateFormat sdf2 = new SimpleDateFormat(fData);
                    Date day = new Date();

                    lblHora.setText(getDiaSemana() + ", " + sdf2.format(day) + " | " + sdf.format(hour));
                }
            }
        });
        t.start();
        limpaCampos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tfCodigo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        lblFoto = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblCidade = new javax.swing.JLabel();
        lblNome = new javax.swing.JLabel();
        lblTitulacao = new javax.swing.JLabel();
        btMarcarPonto = new javax.swing.JButton();
        btJustificativa = new javax.swing.JButton();
        lblJust = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        lblHora = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(153, 204, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Código.:");

        tfCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfCodigoKeyPressed(evt);
            }
        });

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblFoto.setBackground(new java.awt.Color(51, 51, 51));
        lblFoto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFoto.setText("* Foto");
        lblFoto.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblFoto.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lblFoto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblFotoMouseClicked(evt);
            }
        });
        jPanel2.add(lblFoto, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 10, 90, 100));

        jLabel2.setText("Nome.:");
        jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel3.setText("Titulação.:");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jLabel4.setText("Cidade.:");
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 70, -1, -1));

        lblCidade.setText("jLabel6");
        jPanel2.add(lblCidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 70, -1, -1));

        lblNome.setText("jLabel6");
        jPanel2.add(lblNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 10, -1, -1));

        lblTitulacao.setText("jLabel6");
        jPanel2.add(lblTitulacao, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 40, -1, -1));

        btMarcarPonto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/os/rh/imagens/confi.gif"))); // NOI18N
        btMarcarPonto.setText("Marcar");
        btMarcarPonto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMarcarPontoActionPerformed(evt);
            }
        });
        btMarcarPonto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btMarcarPontoKeyPressed(evt);
            }
        });

        btJustificativa.setText("F2 -Justificativa");
        btJustificativa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btJustificativaActionPerformed(evt);
            }
        });

        lblJust.setForeground(new java.awt.Color(255, 0, 0));
        lblJust.setText("Justificativa Adicioada!");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/os/rh/imagens/erase-icon.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                        .addComponent(btJustificativa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btMarcarPonto))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 529, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblJust))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btMarcarPonto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btJustificativa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblJust))
        );

        jPanel1.add(jPanel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 39, -1, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("MARCAR PONTO");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 15, -1, -1));

        lblHora.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lblHora.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblHora.setText("jLabel1");
        lblHora.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jPanel1.add(lblHora, new org.netbeans.lib.awtextra.AbsoluteConstraints(259, 11, 280, -1));

        jMenu1.setText("Opções");

        jMenuItem1.setText("Listar Pontos");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lblFotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblFotoMouseClicked
        // TODO add your handling code here:

    }//GEN-LAST:event_lblFotoMouseClicked

    private void tfCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfCodigoKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            FuncionarioDAO fDAO = new FuncionarioDAO();

            try {
                f = fDAO.checkExists("codigoPonto", Integer.parseInt(tfCodigo.getText())).get(0);
            } catch (Exception e) {
            }
            if (f != null) {
                if (f.isAtivo()) {
                    carregarFoto(f.getCaminhoFoto());
                    lblNome.setText(f.getNome());
                    lblCidade.setText(f.getCidade().getDescricao() + " - " + f.getCidade().getEstado().getDescricao());
                    lblTitulacao.setText(f.getTitulacao().getDescricao());
                    btMarcarPonto.setEnabled(true);
                    btMarcarPonto.requestFocus();

                } else {
                    JOptionPane.showMessageDialog(rootPane, "Funcionário não está ativo!");
                    tfCodigo.setText("");
                    tfCodigo.setFocusable(true);
                }
            } else {
                JOptionPane.showMessageDialog(rootPane, "Funcionário não encontrado!");
                tfCodigo.setText("");
                tfCodigo.setFocusable(true);
            }

        } else if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_F2) {
            btJustificativaActionPerformed(null);
        }
    }//GEN-LAST:event_tfCodigoKeyPressed

    private void btMarcarPontoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMarcarPontoActionPerformed
        // TODO add your handling code here:
        String senha = TelaPedeSenha.chamaTela();
        senha = Util.md5(senha);
        if (senha.equals(Ativo.getUsuario().getSenha())) {
            PontoProfessoresDAO pDAo = new PontoProfessoresDAO();
            PontoProfessores p = pDAo.pesquisaPonto(new Date(), f, Ativo.getPeriodo());
            if (p == null) {
                p = new PontoProfessores();
            } else if (p.getHoraEntrada() != null && p.getHoraSaida() != null) {
                p = new PontoProfessores();
            }
            String texto = "";
            p.setProfessor(f);
            p.setData(new Date());
            if (p.getHoraEntrada() == null) {
                p.setHoraEntrada(new Date());
                p.setJustEntrada(justificativa);
                texto += "Entrada de " + f.getNome() + ", marcada com sucesso!";
            } else {
                p.setHoraSaida(new Date());
                p.setJustSaida(justificativa);
                texto += "Saída de " + f.getNome() + ", marcada com sucesso!";
            }
            p.setPeriodo(Ativo.getPeriodo());
            pDAo.salvar(p);
            JOptionPane.showMessageDialog(null, texto);
            limpaCampos();
        } else {
            JOptionPane.showMessageDialog(null, "Senha Incorreta, Impossível Marcar ponto!");
            limpaCampos();
        }


    }//GEN-LAST:event_btMarcarPontoActionPerformed

    private void btJustificativaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btJustificativaActionPerformed
        // TODO add your handling code here:
        justificativa = TelaJustificativaPonto.chamaTela();
        if(!justificativa.equals("")){
            lblJust.setText("Justificativa Adicioada!");
        }
    }//GEN-LAST:event_btJustificativaActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_F2) {
            btJustificativaActionPerformed(null);
        }
    }//GEN-LAST:event_formKeyPressed

    private void btMarcarPontoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btMarcarPontoKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_F2) {
            btJustificativaActionPerformed(null);
        }
    }//GEN-LAST:event_btMarcarPontoKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        limpaCampos();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        TelaListarPontos tlp = new TelaListarPontos();
        tlp.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void limpaCampos() {
        f = null;
        btMarcarPonto.setEnabled(false);
        lblCidade.setText("");
        lblFoto.setIcon(null);
        lblNome.setText("");
        lblTitulacao.setText("");
        tfCodigo.setText("");
        tfCodigo.requestFocus();
        justificativa = "";
        lblJust.setText("");
    }

    private String getDiaSemana() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int day = cal.get(Calendar.DAY_OF_WEEK);
        switch (day) {

            case 1:
                return "Domingo";
            case 2:
                return "Segunda";
            case 3:
                return "Terça";
            case 4:
                return "Quarta";
            case 5:
                return "Quinta";
            case 6:
                return "Sexta";
            case 7:
                return "Sábado";

        }
        return "";
    }

    private void carregarFoto(String path) {
        String caminhoFoto = path;

        ImageIcon imagem = new ImageIcon(caminhoFoto);

        Image img = imagem.getImage().getScaledInstance(lblFoto.getWidth() + 2, lblFoto.getHeight() + 2, Image.SCALE_DEFAULT);
        lblFoto.setIcon(new ImageIcon(img));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaMarcarPonto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaMarcarPonto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaMarcarPonto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaMarcarPonto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaMarcarPonto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btJustificativa;
    private javax.swing.JButton btMarcarPonto;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JLabel lblCidade;
    private javax.swing.JLabel lblFoto;
    private javax.swing.JLabel lblHora;
    private javax.swing.JLabel lblJust;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblTitulacao;
    private javax.swing.JTextField tfCodigo;
    // End of variables declaration//GEN-END:variables
}
